package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVo;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*预约设置*/
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /*批量添加数据*/
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            /*解析表格数据*/
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            if (readExcel != null && readExcel.size() > 0) {
                //    private Date orderDate;//预约设置日期
                //    private int number;//可预约人数
                //    private int reservations ;//已预约人数
                /*创建实体类*/
                ArrayList<OrderSetting> orderSettingArrayList = new ArrayList<>();
                //遍历数组{日期},{人数}
                for (String[] strings : readExcel) {
                    String data = strings[0];   //{日期}
                    String number = strings[1]; //{人数}
                    SimpleDateFormat dateFm = new SimpleDateFormat("yyyy/MM/dd");
                    OrderSetting orderSetting = new OrderSetting(dateFm.parse(data), Integer.parseInt(number));
                    orderSettingArrayList.add(orderSetting);
                }
                orderSettingService.add(orderSettingArrayList);
            }
            return new Result();

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    /*根据月份查询对应的预约设置*/
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String data) {
//参数格式为：2019-03
        try {
            List<OrderSettingVo> lisit = orderSettingService.getOrderSettingByMonth(data);
            return new Result(lisit);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    /*根据单个日期修改预约设置*/
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }

    }
}
